package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factorydata.domain.WarehousePosition;
import com.ruoyi.factorydata.mapper.WarehousePositionMapper;
import com.ruoyi.factorydata.service.IWarehousePositionService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 仓库仓位信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-16
 */
@Service
public class WarehousePositionServiceImpl extends ServiceImpl<WarehousePositionMapper, WarehousePosition> implements IWarehousePositionService {
    private final WarehousePositionMapper warehousePositionMapper;
    private final ISysUserService sysUserService;

    public WarehousePositionServiceImpl(WarehousePositionMapper warehousePositionMapper, ISysUserService sysUserService) {
        this.warehousePositionMapper = warehousePositionMapper;
        this.sysUserService = sysUserService;
    }

    /**
     * 查询仓库仓位信息
     *
     * @param id 仓库仓位信息主键
     * @return 仓库仓位信息
     */
    @Override
    public WarehousePosition selectWarehousePositionById(Long id) {
        return getById(id);
    }

    /**
     * 查询仓库仓位信息列表
     *
     * @param warehousePosition 仓库仓位信息
     * @return 仓库仓位信息
     */
    @Override
    public List<WarehousePosition> selectWarehousePositionList(WarehousePosition warehousePosition) {
        HashMap<Long, String> map = new HashMap<>();
        List<WarehousePosition> warehousePositions = warehousePositionMapper.selectWarehousePositionList(warehousePosition);
        warehousePositions.forEach(item -> {
            Long userId = Long.valueOf(item.getCreateBy());
            String username = map.get(userId);
            if(StringUtils.isBlank( username)){
                SysUser sysUser = sysUserService.selectUserById(userId);
                if(sysUser!=null){
                    username=sysUser.getNickName();
                    map.put(userId, username);
                }
            }
            item.setCreateByName(username);
        });
        return warehousePositions;
    }

    /**
     * 新增仓库仓位信息
     *
     * @param warehousePosition 仓库仓位信息
     * @return 结果
     */
    @Override
    public int insertWarehousePosition(WarehousePosition warehousePosition) {
        warehousePosition.setCreateTime(DateUtils.getNowDate());
        return warehousePositionMapper.insertWarehousePosition(warehousePosition);
    }

    /**
     * 修改仓库仓位信息
     *
     * @param warehousePosition 仓库仓位信息
     * @return 结果
     */
    @Override
    public int updateWarehousePosition(WarehousePosition warehousePosition) {
        warehousePosition.setUpdateTime(DateUtils.getNowDate());
        return warehousePositionMapper.updateWarehousePosition(warehousePosition);
    }

    /**
     * 批量删除仓库仓位信息
     *
     * @param ids 需要删除的仓库仓位信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehousePositionByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除仓库仓位信息信息
     *
     * @param id 仓库仓位信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehousePositionById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public String getByIdAndCache(Long positionId, HashMap<Long, String> positionNameCache) {
        return CacheSupportUtils.getFromCacheOrDB(positionId, positionNameCache, this::selectWarehousePositionById, WarehousePosition::getPosistionName, true);
    }
}